package utils

import (
	"broker/interface/iserver"
	"fmt"
	"gopkg.in/ini.v1"
	"sync"
)

/*
	全局参数
*/
type Config struct {
	/*
		Server
	*/
	TcpServer iserver.IServer //全局Server句柄
	Host      string          //当前主机监听IP
	Port      int             //当前主机监听端口
	Name      string          //主机名
	IPVersion string          //tcp版本
	WebSocketPort string   	  //WebSocket端口

	/*
		iTunnel broker属性
	*/
	Version        string //版本号
	MaxConn        uint32 //最大连接数
	MaxPackageSize uint32 //数据包最大值

	WorkerPool        bool   //是否开启工作池
	WorkerPoolSize    uint32 //Worker工作池的队列的个数
	MaxWorkerTaskSize uint32 //每个worker 对应的消息队列的任务的数量最大值
}

var conf *Config
var once sync.Once

func GetConfig() *Config {
	once.Do(func() {
		conf = (&Config{}).newConfig()
	})
	return conf

}

func (c *Config) newConfig() *Config {
	file, err := ini.Load("src/broker/config/config.ini")
	if err != nil {
		file, err = ini.Load("../config/config.ini")
		if err != nil {
			fmt.Println("配置文件错误 Error:", err)
			panic("配置文件错误")
		}
	}
	c.loadServer(file)
	//c.loadDB(file)
	return c
}
func (c *Config) loadServer(file *ini.File) {
	c.Name = file.Section("server").Key("Name").MustString("default")
	c.Version = file.Section("server").Key("Version").MustString("V0.1")
	c.Host = file.Section("server").Key("Host").MustString("0.0.0.0")
	c.Host = file.Section("server").Key("Host").MustString("0.0.0.0")
	c.Port = file.Section("server").Key("Port").MustInt(8999)
	c.WebSocketPort =  file.Section("server").Key("WebSocketPort").MustString("8083")
	c.MaxConn = uint32(file.Section("server").Key("MaxConn").MustInt(1000))
	c.MaxPackageSize = uint32(file.Section("server").Key("MaxPackageSize").MustInt(4096))
	c.IPVersion = file.Section("server").Key("IPVersion").MustString("tcp4")
	c.WorkerPool = file.Section("server").Key("Host").MustBool(true)
	c.WorkerPoolSize = uint32(file.Section("server").Key("WorkerPoolSize").MustInt(10))
	c.MaxWorkerTaskSize = uint32(file.Section("server").Key("MaxWorkerTaskSize").MustInt(1024))
}
